"""
Problem 94: https://projecteuler.net/problem=94

It is easily proved that no equilateral triangle exists with
integral length sides and integral area. However, the almost
equilateral triangle 5-5-6 has an area of 12 square units.

We shall define an almost equilateral triangle to be a triangle
for which two sides are equal and the third differs by no more
than one unit.

Find the sum of the perimeters of all almost equilateral triangles
with integral side lengths and area and whose perimeters do not
exceed one billion (1,000,000,000).

"""


def triangleArea(a, b, c):
    '''
    >>> assert triangleArea(5,5,6) == 12
    >>> print(triangleArea(302828,302828,302829))
    39709429597.0
    '''
    p = (a+b+c)/2
    return (p*(p-a)*(p-b)*(p-c))**0.5


def almostEquilateralTriangleArea(e):
    '''
    >>> print(almostEquilateralTriangleArea(302828))
    39709429597.0
    '''
    ee = e**2
    return ((3*ee+4*e+1)*(ee-1)/16)**0.5


def solution(perimeterLimit: int = 1000000000) -> int:
    res = 0
    equalSide = 2
    limit = (perimeterLimit-1)//3
    while equalSide <= limit:
        area = almostEquilateralTriangleArea(equalSide)
        if area == int(area):
            print(equalSide, area)
            res += equalSide*3+1
        equalSide+=1

    return res


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    print(solution())
    # 156274530155969160
